#INCLUDE "PROTHEUS.CH"
#INCLUDE "COLORS.CH"
#INCLUDE "TBICONN.CH"

User Function ALPH150()


cTable   := "calculo_SD2icms"
cTable   :=Lower(ctable)
cNomdir  := "\system\"                         //getmv("MV_DIREXP")
cNomeDir := cNomdir+cTable
nHdlTxt  := FCREATE(cNomeDir)
cLentxt  := chr(13)+chr(10)
cNumEmpA := '12120'


nTot1:=0
nTot2:=0
nTotreg :=0

cQuery:='' //QUERY
cQuery := " SELECT R_E_C_N_O_ RECNOX,SD2.* FROM "
cQuery +=    RetSqlName("SD2") + " SD2 WHERE "
cQuery += "  SD2.D_E_L_E_T_ <> '*' "
cQuery += " AND  SD2.D2_EXPALP = ' ' "
cQuery += "	ORDER BY SD2.D2_FILIAL,SD2.D2_DOC+SD2.D2_SERIE+SD2.D2_CLIENTE,SD2.D2_LOJA,SD2.D2_FORMUL"
cQuery:=ChangeQuery(cQuery)

if !empty(Select("TD2"))
	dbSelectArea("TD2")
	dbCloseArea()
endif
//TCQuery cQuery ALIAS TD2 NEW
dbUseArea(.T., "TOPCONN", TCGenQry(,,cQuery), 'TD2', .F., .T.)
tcsetfield("TD2","D2_EMISSAO","D")

DbSelectArea("TD2")
DbGoTop()

_cDoc:=''
_cHVSF2:='' // TD2->D2_FILIAL+TD2->D2_DOC+TD2->D2_SERIE+TD2->D2_CLIENTE+TD2->D2_LOJA+TD2->D2_FORMUL

While !EOF()

	_nVTicmr:=0
	_cHVSF2:=TD2->D2_FILIAL+TD2->D2_DOC+TD2->D2_SERIE+TD2->D2_CLIENTE+TD2->D2_LOJA+TD2->D2_FORMUL
	_cDoc:=TD2->D2_DOC
	
	While !EOF() .and. _cHVSF2 == TD2->D2_FILIAL+TD2->D2_DOC+TD2->D2_SERIE+TD2->D2_CLIENTE+TD2->D2_LOJA+TD2->D2_FORMUL
		
		Dbselectarea("SF4")
		Dbsetorder(1)
		Dbseek(xFILIAL("SF4")+TD2->D2_TES)
		
		IF upper(substr(SF4->F4_TEXTO,1,1)) <> 'V'
			DbSelectArea("TD2")
			Dbskip()
			Loop
		Endif
		_nValicmR:=0
		_nValicmR:=TD2->D2_VALICM
		IF SF4->F4_CRICMPR == 'S' //CALCULA PRESUMIDO
			_cCricmPr:=''
			_cCricmPr:=GETADVFVAl("SB1","B1_CRICMPR",XFILIAL("SB1")+TD2->D2_COD,1,'') //CALCULA PRESUMIDO
			IF _cCricmPr =='S'
				_cAliq:=''
				_cAliq:=ALLTRIM(STR(INT(TD2->D2_PICM)))
				_nAliq:=0
				IF _cAliq == '12'
					_nAliq := 0.6666
				Else
					_nAliq := 0.4286
				Endif
				//			Dbselectarea("SX5)
				//			IF Dbseek(xFilial("SX5")+'75'+_cAliq)
				//				_nAliq:=VAL(substr(SX5->X5_DESCRI,1,5))
				IF _nAliq > 0
					_nValicmR:=TD2->D2_VALICM * _nAliq
					_nValicmR:=TD2->D2_VALICM - _nValicmR
					//				Endif
				Endif
			Endif
		Endif
		
		cCgc:=GETADVFVAl("SA1","A1_CODALPH",XFILIAL("SA1")+TD2->D2_CLIENTE+TD2->D2_LOJA,1,"nao achado")
		_cgc:=If(LEN(ALLTRIM(cCgc))==14, TRANSF(cCgc, "@R 99.999.999/9999-99"), TRANSF(cCgc, "@R 999.999.999-99"))
		_cNomfil:=U_BUSCAFIL(TD2->D2_FILIAL)
		_nTottax:=0
		_nTottax:= _nValicmR+TD2->D2_VALIMP5+TD2->D2_VALIMP6
		
		cTexto :='"' //txt a ser exportado
		cTexto +=  SM0->M0_CODIGO+TD2->D2_FILIAL+'"|"'
		cTexto += _cNomfil     +'"|"'  // 1
		cTexto +=  ALLTRIM(TD2->D2_DOC)                  +'"|"'
		cTexto +=  ALLTRIM(TD2->D2_SERIE)	      +'"|"'
		cTexto +=  _cgc                         +'"|"'
		cTexto +=  TD2->D2_LOJA         +'"|"'
		cTexto +=  TD2->D2_ITEM 	      +'"|"'
		cTexto +=  ALLTRIM(TD2->D2_COD) +'"|"'
		cTexto +=  TD2->D2_UM   	      +'"|'
		
		cValor:=ALLTRIM(STR(TD2->D2_QUANT,14,2))
		cTexto +=  cValor+'|'  // 7
		cValor:=ALLTRIM(STR(((TD2->D2_TOTAL-_nTottax) / TD2->D2_QUANT),11,4))
		cTexto +=  cValor+'|'  // 7
		cValor:=ALLTRIM(STR((TD2->D2_TOTAL-_nTottax),14,2))
		cTexto +=  cValor+'|'  // 7
		cValor:=ALLTRIM(STR(TD2->D2_VALIPI,14,2))
		cTexto +=  cValor+'|'  // 7
		cValor:=ALLTRIM(STR(TD2->D2_VALICM,14,2))
		cTexto +=  cValor+                '|"'  // 7
		cTexto +=  TD2->D2_TES  	      +'"|"'
		cTes:=GETADVFVAl("SF4","F4_TEXTO",XFILIAL("SF4")+TD2->D2_TES,1,"nao achado")
		cTexto +=  ALLTRIM(cTES)  +'"|'
		cValor:=ALLTRIM(STR(TD2->D2_DESC,5,2))
		cTexto +=  cValor+                '|'  // 7
		cValor:=ALLTRIM(STR(TD2->D2_IPI,5,2))
		cTexto +=  cValor+                '|'  // 7
		cValor:=ALLTRIM(STR(TD2->D2_PICM,5,2))
		cTexto +=  cValor+                '|'  // 7
		cValor:=ALLTRIM(STR(TD2->D2_PESO,9,3))
		cTexto +=  cValor+                '|"'  // 7
		cTexto +=  TD2->D2_PEDIDO    +'"|"'
		cTexto +=  TD2->D2_ITEMPV    +'"|"'
		cTexto +=  TD2->D2_LOCAL     +'"|'
		
		_dDate := STRZERO(Month(TD2->D2_EMISSAO),2)+"/"+STRZERO(Day(TD2->D2_EMISSAO),2)+"/"+STRZERO(YEAR(TD2->D2_EMISSAO),4) //11
		cTexto +=  _dDate+'|'  // 12
		
		cValor:=ALLTRIM(STR(TD2->D2_CUSTO1,14,2))
		cTexto +=  cValor+                '|'  // 7
		cValor:=ALLTRIM(STR(TD2->D2_PRUNIT,14,2))
		cTexto +=  cValor+                '|'  // 7
		cValor:=ALLTRIM(STR(TD2->D2_DESCON,14,2))
		cTexto +=  cValor+                '|"'  // 7
		_LOTEB8:=GETADVFVAl("SB8","B8_LOTEFOR",XFILIAL("SB8")+TD2->D2_COD+TD2->D2_LOCAL+TD2->D2_DTVALID+TD2->D2_LOTECTL+TD2->D2_NUMLOTE,1,TD2->D2_LOTECTL)
		cTexto +=  ALLTRIM(_LOTEB8)   +'"|'
		cValor:=ALLTRIM(STR(TD2->D2_VALIMP1,14,2))
		cTexto +=  cValor+                '|'  // 7
		cValor:=ALLTRIM(STR(TD2->D2_VALIMP2,14,2))
		cTexto +=  cValor+                '|'  // 7
		cValor:=ALLTRIM(STR(TD2->D2_VALIMP3,14,2))
		cTexto +=  cValor+                '|'  // 7
		cValor:=ALLTRIM(STR(_nValicmR,14,2))
		cTexto +=  cValor+                '|'  // 7
		cValor:=ALLTRIM(STR(TD2->D2_VALIMP5,14,2))
		cTexto +=  cValor+                '|'  // 7
		cValor:=ALLTRIM(STR(TD2->D2_VALIMP6,14,2))
		cTexto +=  cValor+                '|'  // 7
		cValor:=ALLTRIM(STR(TD2->D2_SEGURO ,14,2))
		cTexto +=  cValor+                '|'  // 7
		cValor:=ALLTRIM(STR(TD2->D2_VALFRE ,14,2))
		cTexto +=  cValor+                '|'  // 7
		cValor:=ALLTRIM(STR(TD2->D2_DESPESA,14,2))
		cTexto +=  cValor+                '|'  // 7
		cValor:=ALLTRIM(STR(TD2->D2_SEGURO,14,2))
		cTexto +=  cValor+                '|'  // 7
		cValor:=ALLTRIM(STR(TD2->D2_VALPS3,14,2))
		cTexto +=  cValor+                '|'  // 7
		cValor:=ALLTRIM(STR(TD2->D2_VALCF3,14,2))
		cTexto +=  cValor+                '|"'  // 7
		
		cTexto +=  'SO'+'"|' //SALES ORDER
		cValor:=ALLTRIM(STR(_nTotTax,14,2))
		cTexto +=  cValor+                '|'  // 7
		cTexto +=  cLentxt
		
		//Grava
		FWrite(nHdlTxt,cTexto,Len(cTexto))
		
		ntot1+=TD2->D2_QUANT
		ntot2+=(TD2->D2_TOTAL - _nTottax)
		nTotreg ++
		Dbselectarea("SD2")
		Dbgoto(TD2->RECNOX)
		Reclock("SD2",.F.)
    	SD2->D2_CRICMPR:=TD2->D2_VALICM - _nValicmR
		Msunlock()
		_nVTicmr:=_nVTicmr + SD2->D2_CRICMPR
		Dbselectarea("TD2")
		Dbskip()
	Enddo
	Dbselectarea("SF2")
	Dbsetorder(1)
	IF Dbseek(_cHVSF2)
		Dbselectarea("SF2")
		Reclock("SF2",.F.)
		SF2->F2_CRICMPR:=_nVTicmr
		mSunlock()
	Endif
	Dbselectarea("TD2")
Enddo
dbSelectArea("TD2")
dbCloseArea()

//DEVOLUCAO


cQuery:='' //QUERY
cQuery := " SELECT R_E_C_N_O_ RECNOX,SD1.* FROM "
cQuery +=    RetSqlName("SD1") + " SD1 WHERE D1_TIPO = 'D' AND "
cQuery += "  SD1.D_E_L_E_T_ <> '*' AND SD1.D1_EXPALP = ' ' "
cQuery += "  Order BY SD1.D1_FILIAL,SD1.D1_DOC,SD1.D1_EMISSAO"
cQuery:=ChangeQuery(cQuery)

if !empty(Select("TD1"))
	dbSelectArea("TD1")
	dbCloseArea()
endif
//TCQuery cQuery NEW ALIAS "TD1"
dbUseArea(.T., "TOPCONN", TCGenQry(,,cQuery), 'TD1', .F., .T.)

tcsetfield("TD1","D1_EMISSAO","D")

DbSelectArea("TD1")
DbGoTop()

While !EOF()
	
	Dbselectarea("SF4")
	Dbsetorder(1)
	Dbseek(xFILIAL("SF4")+TD1->D1_TES)
	
	IF upper(substr(SF4->F4_TEXTO,1,6)) == 'TRANSF'
		DbSelectArea("TD1")
		Dbskip()
		Loop
	Endif
	_nValicmR:=0
	_nValicmR:=TD1->D1_VALICM
	
	IF SF4->F4_CRICMPR == 'S' //CALCULA PRESUMIDO
		_cCricmPr:=''
		_cCricmPr:=GETADVFVAl("SB1","B1_CRICMPR",XFILIAL("SB1")+TD1->D1_COD,1,'') //CALCULA PRESUMIDO
		IF _cCricmPr =='S'
			_cAliq:=''
			_cAliq:=ALLTRIM(STR(INT(TD1->D1_PICM)))
			_nAliq:=0
			Dbselectarea("SX5")
			IF Dbseek(xFilial("SX5")+'75'+_cAliq)
				_nAliq:=VAL(substr(SX5->X5_DESCRI,1,5))
				IF _nAliq > 0
					_nValicmR:=TD1->D1_VALICM * (_nAliq / 100)
					_nValicmR:=TD1->D1_VALICM - _nValicmR
				Endif
			Endif
		Endif
	Endif
	
	cCgc:=GETADVFVAl("SA1","A1_CODALPH",XFILIAL("SA1")+TD1->D1_FORNECE+TD1->D1_LOJA,1,"nao achado")
	_cgc:=If(LEN(ALLTRIM(cCgc))==14, TRANSF(cCgc, "@R 99.999.999/9999-99"), TRANSF(cCgc, "@R 999.999.999-99"))
	_cNomfil:=U_BUSCAFIL(TD1->D1_FILIAL)
	_nTottax:=0
	_nTottax:=_nValicmR+TD1->D1_VALIMP5+TD1->D1_VALIMP6
	
	cTexto :='"' //txt a ser exportado
	cTexto +=  SM0->M0_CODIGO+TD1->D1_FILIAL+'"|"'
	cTexto += _cNomfil     +'"|"'  // 1
	cTexto +=  ALLTRIM(TD1->D1_DOC)                  +'"|"'
	cTexto +=  ALLTRIM(TD1->D1_SERIE)	      +'"|"'
	cTexto +=  _cgc                         +'"|"'
	cTexto +=  TD1->D1_LOJA         +'"|"'
	cTexto +=  SUBSTR(TD1->D1_ITEM,3,2) 	      +'"|"'
	cTexto +=  ALLTRIM(TD1->D1_COD) +'"|"'
	cTexto +=  TD1->D1_UM   	      +'"|'
	
	cValor:=ALLTRIM(STR(TD1->D1_QUANT,14,2))
	cTexto += '-'+cValor+'|'  // 7
	cValor:=ALLTRIM(STR(((TD1->D1_TOTAL-_nTotTax) / TD1->D1_QUANT),11,4))
	cTexto += '-'+cValor+'|'  // 7
	cValor:=ALLTRIM(STR((TD1->D1_TOTAL-_nTottax),14,2))
	cTexto += '-'+cValor+'|'  // 7
	cValor:=ALLTRIM(STR(TD1->D1_VALIPI,14,2))
	cTexto +=  '-'+cValor+'|'  // 7
	cValor:=ALLTRIM(STR(TD1->D1_VALICM,14,2))
	cTexto +=  '-'+cValor+                '|"'  // 7
	cTexto +=  TD1->D1_TES  	      +'"|"'
	cTes:=GETADVFVAl("SF4","F4_TEXTO",XFILIAL("SF4")+TD1->D1_TES,1,"nao achado")
	cTexto +=  ALLTRIM(cTES)  +'"|'
	cValor:=ALLTRIM(STR(TD1->D1_DESC,5,2))
	cTexto +=  '-'+cValor+                '|'  // 7
	cValor:=ALLTRIM(STR(TD1->D1_IPI,,5,2))
	cTexto +=  +cValor+                '|'  // 7
	cValor:=ALLTRIM(STR(TD1->D1_PICM,,5,2))
	cTexto +=  +cValor+                '|'  // 7
	cValor:=ALLTRIM(STR(TD1->D1_PESO,9,3))
	cTexto +=  '-'+cValor+                '|"'  // 7
	cTexto +=  TD1->D1_PEDIDO    +'"|"'
	cTexto +=  TD1->D1_ITEMPV    +'"|"'
	cTexto +=  TD1->D1_LOCAL     +'"|'
	
	_dDate := STRZERO(Month(TD1->D1_EMISSAO),2)+"/"+STRZERO(Day(TD1->D1_EMISSAO),2)+"/"+STRZERO(YEAR(TD1->D1_EMISSAO),4) //11
	cTexto +=  _dDate+'|'  // 12
	
	cValor:=ALLTRIM(STR(TD1->D1_VUNIT,14,2))
	cTexto +=  '-'+cValor+                '|'  // 7
	cValor:=ALLTRIM(STR(TD1->D1_VUNIT,14,2))
	cTexto +=  '-'+cValor+                '|'  // 7
	cValor:=ALLTRIM(STR(TD1->D1_VALDESC,14,2))
	cTexto += '-'+cValor+                '|"'  // 7
	_LOTEB8:=GETADVFVAl("SB8","B8_LOTEFOR",XFILIAL("SB8")+TD1->D1_COD+TD1->D1_LOCAL+TD1->D1_DTVALID+TD1->D1_LOTECTL+TD1->D1_NUMLOTE,1,TD1->D1_LOTECTL)
	cTexto +=  ALLTRIM(_LOTEB8)   +'"|'
	cValor:=ALLTRIM(STR(TD1->D1_VALIMP1,14,2))
	cTexto +=  '-'+cValor+                '|'  // 7
	cValor:=ALLTRIM(STR(TD1->D1_VALIMP2,14,2))
	cTexto +=  '-'+cValor+                '|'  // 7
	cValor:=ALLTRIM(STR(TD1->D1_VALIMP3,14,2))
	cTexto +=  '-'+cValor+                '|'  // 7
	cValor:=ALLTRIM(STR(_nValicmR,14,2))
	cTexto +=  '-'+cValor+                '|'  // 7
	cValor:=ALLTRIM(STR(TD1->D1_VALIMP5,14,2))
	cTexto +=  '-'+cValor+                '|'  // 7
	cValor:=ALLTRIM(STR(TD1->D1_VALIMP6,14,2))
	cTexto +=  '-'+cValor+                '|'  // 7
	cValor:=ALLTRIM(STR(TD1->D1_SEGURO ,14,2))
	cTexto +=  '-'+cValor+                '|'  // 7
	cValor:=ALLTRIM(STR(TD1->D1_VALFRE ,14,2))
	cTexto +=  '-'+cValor+                '|'  // 7
	cValor:=ALLTRIM(STR(TD1->D1_DESPESA,14,2))
	cTexto +=  '-'+cValor+                '|'  // 7
	cValor:=ALLTRIM(STR(TD1->D1_SEGURO,14,2))
	cTexto +=  '-'+cValor+                '|'  // 7
	cValor:=ALLTRIM(STR(TD1->D1_VALPS3,14,2))
	cTexto +=  '-'+cValor+                '|'  // 7
	cValor:=ALLTRIM(STR(TD1->D1_VALCF3,14,2))
	cTexto +=  '-'+cValor+                '|'  // 7
	
	cTexto +=  'CO'+'"|' //SALES ORDER
	cValor:=ALLTRIM(STR(_nTotTax,14,2))
	cTexto +=  '-'+cValor+                '|'  // 7
	cTexto +=  cLentxt
	
	//Grava
	FWrite(nHdlTxt,cTexto,Len(cTexto))
	
	ntot1+=-1*TD1->D1_QUANT
	ntot2+=-1*(TD1->D1_TOTAL-_nValicmR)
	nTotreg ++
	Dbselectarea("TD1")
	Dbskip()
Enddo
fClose(nHdltxt)
dbSelectArea("TD1")
dbCloseArea()
Return



